package com.iplant.ams.serviceImpl.dao.ams;

import com.iplant.ams.service.po.ams.AMSSupplierInfo;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class AMSSupplierInfoDAO extends BaseDAO {

    private static Logger logger = LoggerFactory.getLogger(AMSSupplierInfoDAO.class);

    private static AMSSupplierInfoDAO Instance = null;

    public static AMSSupplierInfoDAO getInstance() {
        if (Instance == null) {
            Instance = new AMSSupplierInfoDAO();
        }
        return Instance;
    }

    /**
     * 新增或更新
     *
     * @param wLoginUser
     * @param wItem
     * @param wErrorCode
     * @return
     */
    public int Update(BMSEmployee wLoginUser, AMSSupplierInfo wItem, OutResult<Integer> wErrorCode) {
        int wResult = 0;

        try {
            if (wItem == null) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return wResult;
            }

            wItem.setUserInfo(wLoginUser);
            wResult = this.UpdatePo(wLoginUser, wItem, wErrorCode);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error(ex.toString());
        }

        return wResult;
    }

    public ServiceResult<Integer> Import(BMSEmployee wLoginUser, List<AMSSupplierInfo> wItemList, OutResult<Integer> wErrorCode) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);

        try {
            if (wItemList == null || wItemList.isEmpty()) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return wResult;
            }

            for (AMSSupplierInfo wItem : wItemList) {
                wItem.setUserInfo(wLoginUser);
                this.UpdatePo(wLoginUser, wItem, wErrorCode);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 单条删除
     *
     * @param wLoginUser
     * @param wItem
     * @param wErrorCode
     * @return
     */
    public ServiceResult<Integer> Delete(BMSEmployee wLoginUser, AMSSupplierInfo wItem, OutResult<Integer> wErrorCode) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);

        try {
            if (wItem == null) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return wResult;
            }

            this.DeletePO(wLoginUser, wItem, wErrorCode);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除
     *
     * @param wLoginUser
     * @param wIDList
     * @param wErrorCode
     * @return
     */
    public ServiceResult<Integer> DeleteList(BMSEmployee wLoginUser, List<Integer> wIDList, OutResult<Integer> wErrorCode) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);

        try {
            if (wIDList == null || wIDList.isEmpty()) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return wResult;
            }
            wIDList.removeIf(p -> p <= 0);
            if (wIDList.isEmpty()) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return wResult;
            }

            // 批量删除SQL语句
            String wSQL = StringUtils.Format(
                    "DELETE FROM {0}.ams_supplier_info WHERE ID in ({1})",
                    super.getDBName(), StringUtils.Join(",", wIDList));
            Map<String, Object> wParamMap = new HashMap<>();
            wSQL = this.DMLChange(wSQL);
            wResult.Result = super.Update(wSQL, wParamMap);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询集合
     *
     * @param wLoginUser
     * @param wIDs
     * @param wName
     * @param wCode
     * @param wCreatorID
     * @param wActive
     * @param wContactName
     * @param wContactNumber
     * @param wOpeningBank
     * @param wPaymentAccount
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @param wErrorCode
     * @return
     */
    public List<AMSSupplierInfo> SelectList(
            BMSEmployee wLoginUser, List<Integer> wIDs, String wName, String wCode, int wCreatorID, int wActive,
            String wContactName, String wContactNumber, String wOpeningBank, String wPaymentAccount,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<AMSSupplierInfo> wResultList = new ArrayList<>();

        try {

            // ID List
            if (wIDs == null) {
                wIDs = new ArrayList<>();
            }
            wIDs.removeIf(p -> p <= 0);

            // 时间的处理
            Calendar wBaseTime = CalendarTool.getBasicDate();
            wBaseTime.set(2000, 1, 1);
            if (wStartTime == null || wStartTime.compareTo(wBaseTime) < 0) {
                wStartTime = wBaseTime;
            }
            if (wEndTime == null || wEndTime.compareTo(wBaseTime) < 0) {
                wEndTime = wBaseTime;
            }
            if (wStartTime.compareTo(wEndTime) > 0) {
                return wResultList;
            }

            String wSQL = StringUtils.Format(
                    "SELECT t1.* FROM {0}.ams_supplier_info t1 WHERE ID > 0 " +
                            " AND (:ID = '''' or t1.ID IN (''{1}'')) " +
                            " AND (:Name = '''' OR t1.Name like :Name) " +
                            " AND (:Code = '''' OR t1.Code like :Code) " +
                            " AND (:CreatorID <= 0 OR t1.CreatorID = :CreatorID ) " +
                            " AND (:Active < 0 OR t1.Active = :Active) " +
                            " AND (:ContactName = '''' OR t1.ContactName like :ContactName) " +
                            " AND (:ContactNumber = '''' OR t1.ContactNumber like :ContactNumber) " +
                            " AND (:OpeningBank = '''' OR t1.OpeningBank like :OpeningBank) " +
                            " AND (:PaymentAccount = '''' OR t1.PaymentAccount like :PaymentAccount) " +
                            " AND (:StartTime <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') OR :StartTime <= t1.CreateTime) " +
                            " AND (:EndTime <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') OR :EndTime >= t1.CreateTime) ",
                    super.getDBName(), StringUtils.Join("','", wIDs));

            Map<String, Object> wParamMap = new HashMap<>();
            wParamMap.put("ID", StringUtils.Join(",", wIDs));
            wParamMap.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));
            wParamMap.put("Code", StringUtils.isEmpty(wCode) ? "" : ("%" + wCode + "%"));
            wParamMap.put("CreatorID", wCreatorID);
            wParamMap.put("Active", wActive);
            wParamMap.put("ContactName", StringUtils.isEmpty(wContactName) ? "" : ("%" + wContactName + "%"));
            wParamMap.put("ContactNumber", StringUtils.isEmpty(wContactNumber) ? "" : ("%" + wContactNumber + "%"));
            wParamMap.put("OpeningBank", StringUtils.isEmpty(wOpeningBank) ? "" : ("%" + wOpeningBank + "%"));
            wParamMap.put("PaymentAccount", StringUtils.isEmpty(wPaymentAccount) ? "" : ("%" + wPaymentAccount + "%"));
            wParamMap.put("StartTime", wStartTime);
            wParamMap.put("EndTime", wEndTime);

            wSQL = this.DMLChange(wSQL);

            wResultList = this.QueryForList(wSQL, wParamMap, wPagination, AMSSupplierInfo.class);
        } catch (Exception ex) {
            wErrorCode.set(Integer.valueOf(ExceptionTypes.DBSQL.getValue()));
            logger.error(ex.toString());
        }

        return wResultList;
    }

    /**
     * 条件查询
     *
     * @param wLoginUser
     * @param wParams
     * @param wPagination
     * @param wErrorCode
     * @return
     */
    public List<AMSSupplierInfo> SelectList(BMSEmployee wLoginUser,
            Map<String, Object> wParams, Pagination wPagination, OutResult<Integer> wErrorCode) {
        return this.SelectPOList(wLoginUser, wParams, AMSSupplierInfo.class, wPagination, wErrorCode);
    }
}
